//
// Created by wxd on 2025/9/11.
//

#include "Object.h"

void Object::addChild(Object *child) {
    auto res = std::find(mChilds.begin(), mChilds.end(), child);

    if (res == mChilds.end()) {
        mChilds.push_back(child);
        child->mParent = this;
    }
}

std::vector<Object *> Object::getChilds() const {
    return mChilds;
}

Object *Object::getParent() const {
    return mParent;
}

glm::mat4 Object::getModelMatrix() {
    auto parentWorldMatrix = glm::mat4(1.0f);

    if (mParent) {
        parentWorldMatrix = mParent->getModelMatrix();
    }

    auto transform = glm::mat4(1.0f);

    transform = glm::scale(transform, mScale);

    transform = glm::rotate(transform, glm::radians(mRotation.x), glm::vec3(1.f, 0.f, 0.f));
    transform = glm::rotate(transform, glm::radians(mRotation.y), glm::vec3(0.f, 1.f, 0.f));
    transform = glm::rotate(transform, glm::radians(mRotation.z), glm::vec3(0.f, 0.f, 1.f));

    transform = parentWorldMatrix * glm::translate(glm::mat4(1.f), mPosition) * transform;
    return transform;
}